#pragma once
#include <iostream>
#include <string>
#include <vector>

const std::string HttpSep = "\r\n";
class HttpRequest
{
public:
    HttpRequest() : _req_blank(HttpSep)
    {
    }

    ~HttpRequest() {}
    bool Getline(std::string &request, std::string *line)
    {
        auto pos = request.find(HttpSep);
        if (pos == std::string::npos)
            return false;
        *line = request.substr(0, pos);
        request.erase(0, pos + HttpSep.size());
        return true;
    }

    void Deserialize(std::string &request) // 将浏览器发送到服务器的请求进行反序列化
    {
        std::string line;
        if (!Getline(request, &line))
            return;
        _req_line = line;

        while (true)
        {
            bool ok = Getline(request, &line);
            if (ok && line.empty())
            {
                _req_content = line;
                break;
            }
            else if (ok && !line.empty())
            {
                _req_header.push_back(line);
            }
            else
                break;
        }
        return;
    }

    void DebugHttp()
    {
        std::cout << "_req_line: " << _req_line << std::endl;
        for (auto &line : _req_header)
        {
            std::cout << "----> " << line << std::endl;
        }
        std::cout << "_req_blank: " << _req_blank << std::endl;
        std::cout << "_req_content: " << _req_content << std::endl;
    }

private:
    std::string _req_line;
    std::vector<std::string> _req_header;
    std::string _req_blank;
    std::string _req_content;
};